/**
 * @description  关键利率久期
 * @author zls
 */
import React, { PureComponent } from 'react';
import { message } from 'antd';
import { DATA_ENTRY, NoQueryC, AdvancedSearch, PrimaryC, ContentPackage } from '@/src/comps';
import { connect } from 'dva';
import moment from 'moment';
import CurveSetting from './curveSetting';
import Overview from './overview'; //概览
import Detail from './detail'; //明细

class KeyRateDurations extends PureComponent {
  constructor() {
    super();
    this.state = {
      curveSettings: ['O/N', '1M', '2M', '3M', '6M', '9M', '1Y', '2Y', '3Y', '5Y', '7Y', '8Y', '9Y', '10Y', '15Y', '20Y', '30Y', '40Y', '50Y'].map((x) => {
        return { name: x, value: 1.0 };
      }),
      visible: false, //模态框
      activeKey: '1',
      portfolioCode: '',
      tradingDate: '', //上一交易日
      searchState: false, //搜索条件是否改变
      searchData: null,
    };
  }
  componentDidMount() {
    this.props.dispatch({
      type: 'tradeDateM/getTradingDate',
      payload: { type: '1' },
      callback: (res) => {
        this.setState({ tradingDate: res.data });
      },
    });
  }

  //组合和产品改变
  handPortfolioCodeChange = (e) => {
    console.log(e, '----');
    if (e) {
      const info = e.info;
      this.setState({ portfolioCode: info.code, searchState: true });
    } else {
      this.setState({ portfolioCode: '', searchState: true });
    }
  };

  // 查询
  onSearchData = (e) => {
    const { portfolioCode, curveSettings } = this.state;
    const { benchMark, endDate } = e;

    if (!portfolioCode) {
      message.warning('请选择产品');
      return;
    }
    // if (!benchMark) {
    //     message.warning("请选择基准");
    //     return
    // }
    if (!endDate) {
      message.warning('请选择时间区间');
      return;
    }
    this.setState({
      searchData: {
        portfolioCode: portfolioCode,
        benchMark: benchMark,
        endDate: endDate.format('YYYY-MM-DD'),
        curveSettings: curveSettings?.reduce((obj, cur, index) => {
          obj[cur.name] = cur.value;
          return obj;
        }, {}),
      },
      searchState: false,
    });
  };
  onOk = (e) => {
    this.setState({ visible: false, curveSettings: e }, () => {
      if (this.state.searchData) {
        if (this.state.searchState) {
          message.warning('曲线设置条件已变更，请重新执行查询。');
        } else {
          this.onSearchData({
            benchMark: this.state.searchData?.benchMark,
            endDate: moment(this.state.searchData?.endDate),
          });
        }
      }
    });
  };

  render() {
    const searchProps = {
      dataSource: [
        {
          dataIndex: 'portfolioCode',
          title: '产品名称',
          type: DATA_ENTRY.PRODUCTLIST,
          multiple: false,
          dataType: 2,
          onChange: (e) => this.handPortfolioCodeChange(e),
        },
        {
          dataIndex: 'benchMark',
          title: '基准',
          type: DATA_ENTRY.BENCHMARK,
          onChange: () => this.setState({ searchState: true }),
        },
        {
          dataIndex: 'endDate',
          title: '截止日期',
          type: DATA_ENTRY.SINGLEDATE,
          disableEnd: moment(this.state.tradingDate),
          initialValue: moment(this.state.tradingDate).subtract(1, 'days'),
        },
      ],
      onQuery: (e) => this.onSearchData(e),
      onClear: () => {
        this.setState({
          portfolioCode: '',
          searchState: true,
        });
      },
    };
    return (
      <ContentPackage
        title='关键利率久期'
        contentHtml={
          <div>
            <div>
              <AdvancedSearch {...searchProps} />
            </div>

            <div style={{ marginTop: '10px' }}>
              <PrimaryC title='曲线设置' onClick={() => this.setState({ visible: true })} />
            </div>
            {/* 曲线设置弹窗 */}
            <CurveSetting
              visible={this.state.visible}
              defaultCurveSettings={this.state.curveSettings}
              onOk={(e) => this.onOk(e)}
              onCancel={() => this.setState({ visible: false })}
            ></CurveSetting>
            {this.state.searchData ? (
              <div>
                <div>
                  <Overview searchData={this.state.searchData}></Overview>
                  <Detail searchData={this.state.searchData}></Detail>
                </div>
              </div>
            ) : (
              <NoQueryC />
            )}
          </div>
        }
      />
    );
  }
}
export default connect(({}) => ({}))(KeyRateDurations);
